﻿using System;
using System.Threading.Tasks;

namespace SingletonPattern
{
    class Program
    {
        static void Main(string[] args)
        {
			Console.WriteLine("Hello SingletonPattern!");

			{
				// 开启多线程测试
				for (int i = 0; i < 5; i++)
                {
					//新建一个Task（Task工厂创建，自动启动）
					Task t1 = Task.Factory.StartNew(() => {
					    string hi = EagerSingleton.GetInstance().Say();
                        Console.WriteLine(hi);
					});

					Task t2 = Task.Factory.StartNew(() => {
						string hi = LazySingleton.GetInstance().Say();
						Console.WriteLine(hi);
					});
				}
			}

            {
				//创建四个LoadBalancer对象
				LoadBalancer balancer1, balancer2, balancer3;
				balancer1 = LoadBalancer.GetLoadBalancer();
				balancer2 = LoadBalancer.GetLoadBalancer();
				balancer3 = LoadBalancer.GetLoadBalancer();

				//判断服务器负载均衡器是否相同
				if (balancer1 == balancer2 && balancer2 == balancer3)
				{
					Console.WriteLine("服务器负载均衡器具有唯一性！");
				}

				//增加服务器
				for (int i = 0; i < 3; i++)
				{
					balancer1.AddServer($"Server {i + 1}");
				}

				//模拟客户端请求的分发，如果输出结果全为同一个server，可以将i适当放大，例如改为"i < 100"
				for (int i = 0; i < 10; i++)
				{
					string server = balancer1.GetServer();
					Console.WriteLine($"分发请求至服务器：{server}");
				}
			}

			{
				// 队列 Queue 助手
				Random random = new Random();
				int count = 11;
                for (int i = 0; i < count; i++)
                {
					OrderInfo orderInfo = new OrderInfo
					{
						OrderNo = $"No.{i+1}",
						ProductNo = $"No.{i + 1}",
						UserId = $"张氏{i + 1}",
						UnitPrice = random.Next(1, count),
						ProductCount = random.Next(1, count)
					};
					QueueHelper<OrderInfo>.Instance.Enqueue(orderInfo);
				}

				if (QueueHelper<OrderInfo>.Instance.QueueCount == count)
                {
                    Console.WriteLine($"Queue队列存入数据量符合预期，数量{count}条.");
                }

				var (hasEntity, entity) = QueueHelper<OrderInfo>.Instance.TryDequeue();
                if (hasEntity)
                {
					Console.WriteLine($"【Queue队列尝试出队】商品信息：OrderNo=>{entity.OrderNo},ProductName=>{entity.ProductNo},UserName=>{entity.UserId},UnitPrice=>{entity.UnitPrice},ProductCount=>{entity.ProductCount},剩余待处理的订单量：{QueueHelper<OrderInfo>.Instance.QueueCount}");
				}

				OrderInfo t;
                do
                {
				    t = QueueHelper<OrderInfo>.Instance.Dequeue();
                    if (t != null)
                    {
						int kjs = random.Next(1,5);

                        if (t.UnitPrice >= kjs && t.ProductCount >= kjs)
                        {
							t.UnitPrice -= kjs;
							t.ProductCount -= kjs;
							Console.WriteLine($"【正常扣减库存操作】商品信息：OrderNo=>{t.OrderNo},ProductName=>{t.ProductNo},UserName=>{t.UserId},UnitPrice=>{t.UnitPrice},ProductCount=>{t.ProductCount},随机扣减变量：{kjs}");
                        }
                        else
                        {
							Console.WriteLine($"【异常数据】商品信息：OrderNo=>{t.OrderNo},ProductName=>{t.ProductNo},UserName=>{t.UserId},UnitPrice=>{t.UnitPrice},ProductCount=>{t.ProductCount},随机扣减变量：{kjs}");
						}
					}
				} while (t != null);
			}

			Console.Read();
		}
    }

	class OrderInfo
	{
		/// <summary>
		/// 订单编号
		/// </summary>
		public string OrderNo { get; set; }
		/// <summary>
		/// 商品编号
		/// </summary>
		public string ProductNo { get; set; }
		/// <summary>
		/// 用户Id
		/// </summary>
		public string UserId { get; set; }
		/// <summary>
		/// 单价
		/// </summary>
		public decimal UnitPrice { get; set; }
		/// <summary>
		/// 商品数量
		/// </summary>
		public int ProductCount { get; set; }
	}

}
